<?php

namespace App\Models\V1;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Zizaco\Entrust\Traits\EntrustUserTrait;
use App\Scopes\V1\ValidDataScope;
use Laravel\Passport\HasApiTokens;

class AdminUser extends Authenticatable
{
    use Notifiable;
    use EntrustUserTrait;
    use HasApiTokens;

    /**
     * 模型启动函数
     */
    protected static function boot()
    {
        parent::boot();
        static::addGlobalScope(new ValidDataScope);
    }

    /**
     * 可被批量赋值字段（白名单）
     *
     * @var array
     */
    protected $fillable = [
        'code', 'password', 'is_staff', 'remember_token',
    ];

    /**
     * 限制数组或JSON输出时，隐藏字段
     *
     * @var array
     */
    protected $hidden = [
        'id', 'password', 'remember_token', 'deleted_at',
    ];


    public function findForPassport($username)
    {
        return AdminUser::where('code',$username)->first();
    }
    /**
     * 获取登录用户对应的教职工信息
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function faculty()
    {
        return $this->belongsTo(Faculty::class, 'code', 'code');
    }

    /**
     * 获取登录用户对应的学生信息
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function student()
    {
        return $this->belongsTo(NewStudent::class, 'exam_code', 'code');
    }

    /**
     * 获取关联到用户忘记密码信息
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function forgetPassword()
    {
        return $this->hasMany(ForgetPassword::class, 'code', 'code');
    }


    /***
     * 获取用户的角色
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function roles()
    {
        return $this->belongsToMany(Role::class,'role_user','user_id');
    }


}
